package com.nf.health.app.activity.global;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.SlideRuler;
import com.nf.health.app.utils.PreferenceHelper;

/** 
* @ClassName: RegisterWeightActivity 
* @Description: 注册  体重
* @author Ian 
* @date 2015-11-4 上午11:02:34 
*  
*/
public class RegisterWeightActivity extends BaseActivity {

	private Button next_btn;
	private String sex;
	private ImageView sex_img;

	private TextView titlebar_title_tv;
	private String nick_name;
	private String type = "";
	private HorizontalScrollView hsv;
	private SlideRuler ruler;
	private int weight = 50;
	private String height;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_register_weight);
		type = getIntent().getStringExtra("type");
		initView();
		initListener();
	}

	public void initView() {
		titlebar_title_tv = (TextView) this.findViewById(R.id.titlebar_title_tv);
		titlebar_title_tv.setText("体重");
		sex_img = (ImageView) this.findViewById(R.id.sex_img);
		if ("1".equals(type)) {
			weight = getIntent().getIntExtra("value", weight);
			sex = PreferenceHelper.getString(PreferenceHelper.USERSEX, "1");
		} else {
			sex = getIntent().getStringExtra("sex");
		}

		nick_name = getIntent().getStringExtra("nick_name");
		height = getIntent().getStringExtra("height");
		if ("1".equals(sex)) {
			sex_img.setBackgroundResource(R.drawable.ka2);
		} else {
			sex_img.setBackgroundResource(R.drawable.ka);
		}
		next_btn = (Button) this.findViewById(R.id.next_btn);
		if ("1".equals(type)) {
			next_btn.setText("确定");
		}
		
		hsv = (HorizontalScrollView) this.findViewById(R.id.hsv);
		ruler = (SlideRuler) findViewById(R.id.ruler);
		
		
		hsv.post(new Runnable() {
			
			@Override
			public void run() {
				setValue(weight);
			}

			private void setValue(int defaultValue) {
				hsv.smoothScrollTo((int)((defaultValue - ruler.getStartValue()) * ruler.getUnitWidth() - hsv.getWidth() / 2 + ruler.getDelay()), hsv.getScrollY());
			}
		});
	}
	private void initListener() {
		next_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if ("1".equals(type)) {
					provider.requestUpdateUser(null, null, null,
							weight + "", null, null,null,null,null,"updataUser");
				} else {
					Intent intent = new Intent(RegisterWeightActivity.this,
							RegisterBirthdayActivity.class);
					intent.putExtra("sex", sex);
					intent.putExtra("nick_name", nick_name);
					intent.putExtra("height", height);
					intent.putExtra("type", type);
					intent.putExtra("weight", weight+"");
					startActivity(intent);
				}
			}
		});
		hsv.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_UP:
					final float totalLength = (float) hsv.getChildAt(0)
							.getWidth() - hsv.getWidth();
					final float halfWidth = (float) hsv.getWidth() / 2;

					float value = 0;// 计算滚动的块数

					value = (halfWidth + hsv.getScrollX() - ruler.getDelay()) / totalLength * ruler.getTotalCount();
					if ((value - (int) value) > 0.5)
						value = (int) (value + 1);
					else
						value = (int) (value);

					final int val = (int) value;

					v.post(new Runnable() {
						@Override
						public void run() {
							hsv.smoothScrollTo( (int) (val * ruler.getUnitWidth() - halfWidth + ruler.getDelay()), hsv.getScrollY());
							weight = val + ruler.getStartValue();
						}
					});

					break;
				}
				return false;
			}
		});
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("updataUser")) {
			showToast("修改成功！");
			finish();
		}
	}
}
